package com.rec.smile.chemical.echo;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufUtil;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.util.CharsetUtil;

@ChannelHandler.Sharable
public class EchoClientHandler extends SimpleChannelInboundHandler<ByteBuf> {
  /**
   *此方法会在连接到服务器后被调用 
   * */
  public void channelActive(ChannelHandlerContext ctx) {
    ctx.write(Unpooled.copiedBuffer("Netty rocks!", CharsetUtil.UTF_8));
  }
  /**
   *此方法会在接收到服务器数据后调用 
   * */
  public void channelRead0(ChannelHandlerContext ctx, ByteBuf in) {
    System.out.println("Client received: " + ByteBufUtil.hexDump(in.readBytes(in.readableBytes())));
  }
  /**
   *&#x6355;&#x6349;&#x5230;&#x5f02;&#x5e38;
   * */
  public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
    cause.printStackTrace();
    ctx.close();
  }

}